//
//  KLMBaseHomeTableController.swift
//  KLM_GN
//
//  Created by Nic on 2025/3/14.
//

import UIKit

private let titleCellId = String(describing: KLMHomeTitleCell.self)
private let memberCellId = String(describing: KLMHomeMemberCell.self)

class KLMBaseHomeTableController: KLMBaseController {

    let tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .insetGrouped)
        tableView.separatorStyle = .none
        tableView.contentInset = UIEdgeInsets(top: -19, left: 0, bottom: 0, right: 0)
        tableView.sectionHeaderHeight = 16
        tableView.sectionFooterHeight = 0
        return tableView
    }()
    
    var homeItems: [[KLMHomeItem]] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        tableView.register(KLMHomeTitleCell.self, forCellReuseIdentifier: titleCellId)
        tableView.register(KLMHomeMemberCell.self, forCellReuseIdentifier: memberCellId)
        tableView.dataSource = self
        tableView.delegate = self
        
        //刷新
        tableView.mj_header = MJRefreshNormalHeader.init {[weak self] in
            guard let self = self else { return }
            self.loadDataRefreshUI()
        }
    }
    
    override func setNavBarStyle() {
        customNavBarAppearance(barTintColor: .white, titleColor: nil)
    }
    
    func loadDataRefreshUI() {}
    
    func endHeaderRefresh() {
        if let header = self.tableView.mj_header {
            header.endRefreshing()
        }
    }
    
    func tableViewDidSelect(item: KLMHomeItem, at: IndexPath, on: UITableView) -> Void {
        print("点击了\(item.title ?? "")")
    }
}

extension KLMBaseHomeTableController: UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return homeItems.count
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return homeItems[section].count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let item = homeItems[indexPath.section][indexPath.row]
        if item.type == .member {
            let cell = tableView.dequeueReusableCell(withIdentifier: memberCellId, for: indexPath) as! KLMHomeMemberCell
            cell.homeItem = item
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: titleCellId, for: indexPath) as! KLMHomeTitleCell
            cell.homeItem = item
            return cell
        }
    }
}

extension KLMBaseHomeTableController: UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let item = homeItems[indexPath.section][indexPath.row]
        if item.type == .member {
            return 63.0
        }
        return 48.0
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        tableViewDidSelect(item: homeItems[indexPath.section][indexPath.row], at: indexPath, on: tableView)
    }
}
